﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Acsys.Entity;

namespace Acsys
{
    public partial class ChangePasswordForm : Form
    {
        User m_currentUser;

        public ChangePasswordForm(User user)
        {
            InitializeComponent();
            m_currentUser = user;
            textBoxUser.Text = user.CODE;
            textBoxNewPassword.Focus();
        }

        private void buttonChange_Click(object sender, EventArgs e)
        {
            try
            {
                if (isValid())
                {
                    m_currentUser.PASSWORD = textBoxNewPassword.Text;
                    RepositoryFactory.GetInstance().CodeNameRepository().Update(m_currentUser);
                    MessageBox.Show("Password changed", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private bool isValid()
        {
            errorProvider1.Clear();
            bool a = textBoxCurrentPassword.Text == "";
            bool b = textBoxNewPassword.Text == "";
            bool c = textBoxConfirmPassword.Text == "";
            bool d = textBoxConfirmPassword.Text != textBoxNewPassword.Text;
            bool e = textBoxCurrentPassword.Text != m_currentUser.PASSWORD;

            if (a) errorProvider1.SetError(textBoxCurrentPassword, "Please type current password");
            if (b) errorProvider1.SetError(textBoxNewPassword, "Please type new password");
            if (c) errorProvider1.SetError(textBoxConfirmPassword, "Please confirm new password");
            if (d) errorProvider1.SetError(textBoxNewPassword, "New password and Confirm is different");
            if (e) errorProvider1.SetError(textBoxCurrentPassword, "Current Password is different");

            return !a && !c && !b && !d && !e;
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
